package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.DimensionApi;
import com.tanhua.model.mongo.Dimension;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;

import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @description: 测试灵魂————报告 zhengzheng、liuhaijie、zhangfude
 * @author: 18321
 * @date: 2021/12/25 16:53
 */
@DubboService
public class DimensionApiImpl implements DimensionApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 提交测试问卷 zhengzheng、liuhaijie、zhangfude
     * @param dimension 维度报告
     * @return 报告Id
     */
    @Override
    public String saveQuestionnaire(Dimension dimension) {
        return mongoTemplate.insert(dimension).getId().toHexString();
    }

    /**
     * 根据报告id查询报告表
     * @author:HeShuai,PanXinFu,LiZhen
     * @param id
     * @return
     */
    @Override
    public Dimension findById(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query,Dimension.class);
    }

    /**
     * 根据维度查询相关用户的动态
     * @author:HeShuai,PanXinFu,LiZhen
     * @param cover
     * @return
     */
    @Override
    public List<Dimension> findByScore(String cover,Long userId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("cover").is(cover).and("userId").ne(userId));
        return mongoTemplate.find(query,Dimension.class);
    }

    /**
     * 更新cover
     * @param cover
     * @param id
     */
    @Override
    public void updateCover(String cover, String id) {
        Query query=new Query(Criteria.where("id").is(id));
        Update update=new Update();
        update.set("cover",cover);
        mongoTemplate.updateFirst(query,update,Dimension.class);
    }

    /**
     * 查询对应用户的历史测试记录
     * @param userId 对应用户id
     * @return 当前用户最新的测试结果
     */
    @Override
    public Map<Long, Dimension> findUserHistory(Long userId) {
        //查询用户最后一次初级问卷测试结果
        Dimension primary = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(1)).with(Sort.by(Sort.Order.desc("createDate"))).limit(1), Dimension.class);
        //查询用户最后一次中级问卷测试结果
        Dimension junior = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(2)).with(Sort.by(Sort.Order.desc("createDate"))).limit(1), Dimension.class);
        //查询用户最后一次高级问卷测试结果
        Dimension senior = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(3)).with(Sort.by(Sort.Order.desc("createDate"))).limit(1), Dimension.class);
        Map<Long, Dimension> map = new HashMap<>();
        if(primary != null){
            map.put(1L, primary);
        }
        if(junior != null){
            map.put(2L, junior);
        }
        if(senior != null){
            map.put(3L, senior);
        }
        return map;
    }
}
